/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AgregarFichero.java
 *
 * Created on 24-may-2011, 18:31:58
 */

package kinesiologia;

import ClasesKinesiologia.Ficha;
import ClasesKinesiologia.Ficheros;
import ClasesKinesiologia.Paciente;
import ClasesKinesiologia.Seciones;
import ClasesKinesiologia.cbPro;
import ClasesKinesiologia.coneccion;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Adrian
 */
public class AgregarFichero extends javax.swing.JFrame {

    private Ficheros fic;
    private cbPro cb;
    private Ficha fAct;
    private DefaultListModel fo;
    private Paciente pAct;
    private List<Seciones> se;
    private List<String> Medicos;
    private boolean nuevo,guardar;
    private Seciones seact=null;
    private int pos,coo;
    
    public AgregarFichero(Paciente pp) {
        initComponents();
        pAct=pp;
        this.lbUltSes.setText("");
        this.lbRes.setText("0");
        se = new ArrayList<Seciones>();
        Medicos = new ArrayList<String>();
        nuevo = true;

        this.cbMedico.removeAllItems();;
        this.cbMedico.addItem("");
        Calendar hoy = Calendar.getInstance();
        this.dateSeciones.setCalendar(hoy);
        guardar = false;
        coo =0;
        this.dateIng.setCalendar(Calendar.getInstance());
        this.lbtodas.setText("0");
        this.lbpos.setText("0");
        fic = new Ficheros();
        cb = new cbPro();
        fo = new DefaultListModel();
        this.lFormularios.setModel(fo);
        //busca Formularios
        fic.cargaFormu(fo,pAct.getDoc(),this.getTitle());
        coneccion conexion = new coneccion();
        ResultSet pas = conexion.hacerConsultaSelect("select * from medico");
        try {
            this.cbMedico.removeAllItems();
            while (pas.next() == true)
            {
               cbMedico.addItem(pas.getString("nombre_medico"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(AgregarFichero.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            conexion.cerrarConeccion();
        }
    }


    public void cargaFic(String nom)
    {
       
        coneccion conexion = new coneccion();
        Ficha aux = null;
        ResultSet pas = conexion.hacerConsultaSelect("select * from ficha where dni_Paciente="+pAct.getDoc());
        try {
            while(pas.next()==true)
            {
            if(pas.getString("nombre_ficha").equals(nom)== true)
            {
                aux = new Ficha(pas.getString(""), pas.getString(""), pas.getDate("fecha_ingreso"),pas.getInt("cod_ficha"));
            }

            }
        } catch (SQLException ex) {
            Logger.getLogger(AgregarFichero.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            conexion.cerrarConeccion();
        }

        
        this.tbDiagnostico.setText(aux.getDiag());
        this.cbMedico.setSelectedItem(aux.getDoc());
        fAct = aux;
        this.dateIng.setDate(aux.getFechaIng());
        guardar= true;
        nuevo = false;
        this.setTitle(nom);
        int tot=0;
        pas = conexion.hacerConsultaSelect("select * from secion where cod_ficha="+String.valueOf(aux.getCod()));
        try {
            while (pas.next() == true)
            {
                Seciones sea = new Seciones(pas.getInt("cod_seciones"),
                        pas.getInt("total"),
                        pas.getInt("hechas"),
                        pas.getDate("fecha_I"),
                        pas.getString("estado"));
                se.add(sea);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AgregarFichero.class.getName()).log(Level.SEVERE, null, ex);
        } finally
        {
            conexion.cerrarConeccion();
        }
        this.lbtodas.setText(String.valueOf(se.size()));
        this.lbTotal.setText(String.valueOf(tot));

       
     }

    private String setfecha(Calendar hoy)
    {
        SimpleDateFormat formato = new SimpleDateFormat( "dd/MM/yyyy");
        return formato.format(hoy.getTime());
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        tbDiagnostico = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lbRes = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        tbCant = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        lbEst = new javax.swing.JLabel();
        btPre = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel10 = new javax.swing.JLabel();
        lbTotal = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        lbpos = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        lbtodas = new javax.swing.JLabel();
        dateSeciones = new com.toedter.calendar.JDateChooser();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lFormularios = new javax.swing.JList();
        jButton6 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        dateIng = new com.toedter.calendar.JDateChooser();
        jLabel13 = new javax.swing.JLabel();
        btObs = new javax.swing.JButton();
        lbUltSes = new javax.swing.JLabel();
        cbMedico = new javax.swing.JComboBox();
        jButton9 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();

        jLabel4.setText("Fecha Ingreso :");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Diagnostico :");

        tbDiagnostico.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbDiagnosticoKeyTyped(evt);
            }
        });

        jLabel3.setText("Fecha Ingreso :");

        jLabel2.setText("Medico : ");

        jLabel5.setText("Fecha ultima Secion :");

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel6.setText("Sesiones");

        jLabel7.setText("Restantes :");

        lbRes.setText("jLabel8");

        jLabel8.setText("Cantidad :");

        tbCant.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbCantActionPerformed(evt);
            }
        });
        tbCant.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbCantKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbCantKeyTyped(evt);
            }
        });

        jButton3.setText("hechas");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Agregar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel9.setText("Estado :");

        lbEst.setText("no entregadas");

        btPre.setText("Seciones Presentadas");
        btPre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPreActionPerformed(evt);
            }
        });

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel10.setText("Total :");

        lbTotal.setText("00");

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/siguiente.gif"))); // NOI18N
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/anterior.gif"))); // NOI18N
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jLabel12.setText("Fecha:");

        lbpos.setText("jLabel13");

        jLabel14.setText("/");

        lbtodas.setText("jLabel15");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(182, 182, 182)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbRes))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateSeciones, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tbCant, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btPre)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(18, 18, 18)
                                .addComponent(lbEst)))))
                .addContainerGap(29, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbpos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel14)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbtodas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(113, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(lbRes))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dateSeciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(tbCant, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton4)
                            .addComponent(jButton3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbTotal)
                            .addComponent(jLabel10)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(lbEst))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btPre)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lbpos)
                        .addComponent(jLabel14)
                        .addComponent(lbtodas)))
                .addGap(25, 25, 25))
        );

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Aceptar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel11.setText("Formularios");

        lFormularios.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lFormularios);

        jButton6.setText("Abrir");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton5.setText("Agregar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 241, Short.MAX_VALUE)
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jButton6))
                .addContainerGap())
        );

        dateIng.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                dateIngKeyTyped(evt);
            }
        });

        jLabel13.setText("Observaciones : ");

        btObs.setText("Agregar/Ver");
        btObs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btObsActionPerformed(evt);
            }
        });

        lbUltSes.setText("jLabel15");

        cbMedico.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbMedico.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbMedicoItemStateChanged(evt);
            }
        });
        cbMedico.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cbMedicoKeyTyped(evt);
            }
        });

        jButton9.setText("Agregar");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 639, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel13)
                                .addGap(15, 15, 15)
                                .addComponent(btObs))
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel1))
                                .addGap(14, 14, 14)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tbDiagnostico, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lbUltSes)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cbMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(dateIng, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(31, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(tbDiagnostico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(dateIng, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jButton9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(lbUltSes, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel13)
                            .addComponent(btObs))
                        .addGap(7, 7, 7)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbCantActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbCantActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tbCantActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:

        int p = Integer.valueOf(this.tbCant.getText());
        int re = Integer.valueOf(this.lbRes.getText());
        if(re>=p)
        {
        seact.agregaSec(p);
        this.tbCant.setText("");
        this.lbRes.setText(seact.getrest());
        coneccion conexion = new coneccion();
        ResultSet pas = conexion.hacerConsultaSelect("select * from secion");

        } else {
            JOptionPane.showMessageDialog(rootPane, "El monto de seciones echas es mayor de las que restan");
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
      if(guarda() ==true) show(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if(nuevo==true)
        {
           int opc = JOptionPane.showConfirmDialog(this, "Desea Salir sin guardar?");
           if(opc==0)
           {
               dispose();
           }
        } else
        {
             dispose();
        }
       
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        int tot = Integer.valueOf(this.tbCant.getText());
        Date fsec = this.dateSeciones.getDate();
        java.sql.Date aux = new java.sql.Date(fsec.getYear(), fsec.getMonth(), fsec.getDay());
        Seciones sec = new Seciones(0,tot,0,aux,"No entregadas");
        seact = sec;
        se.add(sec);
        int to = Integer.valueOf(this.lbTotal.getText());
        to += tot;
        this.tbCant.setText("");
        cargaSec(sec);
        this.lbTotal.setText(String.valueOf(to));
        if(this.se.size()==0)
        {
            pos = 0;
        }
        pos++;
        this.lbpos.setText(String.valueOf(pos));
        coo = Integer.valueOf(lbtodas.getText());
        coo++;
        this.lbtodas.setText(String.valueOf(coo));
        coneccion conexion = new coneccion();

        ResultSet pas = conexion.hacerConsultaSelect("select * from secion");
        try {
            pas.moveToCurrentRow();
            pas.updateInt("cod_seciones", 0);
            pas.updateInt("total", sec.getTot());
            pas.updateInt("hechas", sec.getEchas());
            pas.updateDate("fecha_I", aux);
            pas.updateInt("cod_ficha", this.fAct.getCod());
            pas.updateString("estado-secion", "No Entregada");
            pas.updateInt("estado", 0);
            pas.insertRow();
        } catch (SQLException ex) {
            Logger.getLogger(AgregarFichero.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            conexion.cerrarConeccion();
        }

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
        
        if(coo>0)
        {
            coo--;
            seact = se.get(coo);
            this.cargaSec(seact);
            this.lbpos.setText(String.valueOf((coo+1)));
        }
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
       
        if((coo+1)<se.size())
        {
            coo++;
            seact = se.get(coo);
            this.cargaSec(seact);
            this.lbpos.setText(String.valueOf((coo+1)));
        }
    }//GEN-LAST:event_jButton7ActionPerformed

    private void btPreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPreActionPerformed
        // TODO add your handling code here:
        String p = "entregada";
        this.lbEst.setText(p);
        seact.setEst(p);
      //  fic.guardaSec(seact, fAct, seact.getNom()); actualizar secion
    }//GEN-LAST:event_btPreActionPerformed

    private void tbCantKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbCantKeyTyped
        //
        char n;
        n=evt.getKeyChar();
        if(n!='0' &&
                n!='1' &&
                n!='2' &&
                n!='3' &&
                n!='4' &&
                n!='5' &&
                n!='6' &&
                n!='7' &&
                n!='8' &&
                n!='9') evt.consume();

    }//GEN-LAST:event_tbCantKeyTyped

    private void tbCantKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbCantKeyPressed
        // TODO add your handling code here:

    }//GEN-LAST:event_tbCantKeyPressed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:

       File ruta = new File("");
       File formu = new File(ruta.getAbsolutePath()+"\\Formularios");
       JFileChooser jfc = new JFileChooser(formu.getAbsolutePath());
       int re = jfc.showOpenDialog(dateIng);
       if(re==JFileChooser.OPEN_DIALOG)
       {
           File aux = jfc.getSelectedFile();
           File fi = fic.agregaFormu(pAct, aux);
           fo.addElement(fi.getName());
           lFormularios.setSelectedValue(jfc.getName(fi), true
                   );
       }
      abreFormu();
    }//GEN-LAST:event_jButton5ActionPerformed

    
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        abreFormu();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void btObsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btObsActionPerformed
        // TODO add your handling code here:
        guarda();
        fic.verObs(pAct.getDoc(), fAct);
    }//GEN-LAST:event_btObsActionPerformed

    private void cbMedicoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbMedicoKeyTyped
        // TODO add your handling code here:
        escriveCB(evt,this.cbMedico);
        if(evt.getKeyChar()==8 && ((cbMedico.getSelectedItem().toString().length()-1)==0)
           || cbMedico.getSelectedItem().toString().length()==0)
           {
                for(String o :Medicos)
                {
                 this.cbMedico.addItem(o);
                  }
           }
        
    }//GEN-LAST:event_cbMedicoKeyTyped

    private void tbDiagnosticoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbDiagnosticoKeyTyped
        // TODO add your handling code here:
        nuevo=true;
    }//GEN-LAST:event_tbDiagnosticoKeyTyped

    private void dateIngKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dateIngKeyTyped
        nuevo=true;        // TODO add your handling code here:
    }//GEN-LAST:event_dateIngKeyTyped

    private void cbMedicoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbMedicoItemStateChanged
        // TODO add your handling code here:
        nuevo=true;
    }//GEN-LAST:event_cbMedicoItemStateChanged

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
       cbMedico.removeAllItems();
       cbMedico.addItem("");
       cbMedico.setSelectedItem("");
}//GEN-LAST:event_jButton9ActionPerformed


    private void abreFormu()
    {
        String nom = fo.getElementAt(this.lFormularios.getSelectedIndex()).toString();
        File ruta = new File("");
        File formu =new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+this.pAct.getDoc()+"\\Formularios\\"+nom);
        Runtime r = Runtime.getRuntime();
        try {
            r.exec("explorer.exe "+formu.getCanonicalPath());
        } catch (IOException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    private void cargaSec(Seciones s)
    {
        
        this.dateSeciones.setDate(s.getFe());
        this.lbRes.setText(s.getrest());
        this.lbEst.setText(s.getEst());

    }

   private void escriveCB(java.awt.event.KeyEvent evt,javax.swing.JComboBox esc)
    {
        String p = esc.getSelectedItem().toString();
        if(evt.getKeyChar()!=8)
        {

        p = p+evt.getKeyChar();

        } else
        {
            if(p.length()!=0)
            {
                p = p.substring(0, (p.length() - 1));
            }
        }
        esc.removeAllItems();
        esc.addItem(p);
        esc.setSelectedItem(p);

    }
    public boolean guarda()
    {
     /*   GregorianCalendar fi = (GregorianCalendar) this.dateIng.getCalendar();
        String medico = this.cbMedico.getSelectedItem().toString();
        if(tbDiagnostico.getText().equals("")==false && tbDiagnostico.getText()!=null
          && medico.equals("")==false
          && medico!=null
          && fi!= null)
        {
        Ficha aux = new Ficha(this.tbDiagnostico.getText(),this.cbMedico.getSelectedItem().toString(),fi,null);
        String nom = this.getTitle();
        aux.setNom(nom);
        String lug = fic.guardaFicha(aux, pAct.getDoc());
        aux.setUbi(lug);
        fAct = aux;
        if(buscaMed(medico)==false)
        {
            Medicos.add(medico);
            fic.guardaMedicos(Medicos);
        }
        return true;
        } else {
            JOptionPane.showMessageDialog(rootPane, "Algun campo no ha sido llenado por favor verifiquelo");
            return false;
        }*/return true;
    }

    private boolean buscaMed(String nombre)
    {
         for(String oos:Medicos)
        {
          if(oos.equals(nombre)==true) return true;

        }
         return false;
    }

    /**
    * @param args the command line arguments
    */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btObs;
    private javax.swing.JButton btPre;
    private javax.swing.JComboBox cbMedico;
    private com.toedter.calendar.JDateChooser dateIng;
    private com.toedter.calendar.JDateChooser dateSeciones;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JList lFormularios;
    private javax.swing.JLabel lbEst;
    private javax.swing.JLabel lbRes;
    private javax.swing.JLabel lbTotal;
    private javax.swing.JLabel lbUltSes;
    private javax.swing.JLabel lbpos;
    private javax.swing.JLabel lbtodas;
    private javax.swing.JTextField tbCant;
    private javax.swing.JTextField tbDiagnostico;
    // End of variables declaration//GEN-END:variables

}
